package com.kj.tms.system.controller;


import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.bean.exception.BusinessException;
import com.kj.tms.common.bean.exception.SessionException;
import com.kj.tms.common.bean.session.SessionManager;
import com.kj.tms.common.utils.StringUtil;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.menu.dto.ModuleDto;
import com.kj.tms.pri.system.menu.service.ModuleService;
import com.kj.tms.pri.system.user.dto.UserDto;
import com.kj.tms.pri.system.user.service.UserService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wzl on 2017/11/9.
 * 系统公共的controller，不走拦截器
 */
@Controller
@RequestMapping("/system/register")
public class NonInterceptController {

    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private ModuleService moduleServiceImpl;
    @Autowired
    private UserService userServiceImpl;

    /**
     * 进入到主页面sysWelcome
     *
     * @return
     */
    @RequestMapping(value = "/sysWelcome")
    public ModelAndView sysWelcome() {
        ModelAndView mv = new ModelAndView("system/sys_welcome");
        return mv;
    }

    /**
     * 获取用户菜单信息
     *
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/iniMenu")
    @ResponseBody
    public JSONArray getMenu() {
        JSONArray curUserJsonMenus;
        UserDto user = (UserDto) sessionManager.getAttribute("user");
        if (StringUtil.isNull(user)) {
            throw new BusinessException("登录超时！");
        }
        curUserJsonMenus = moduleServiceImpl.getMenu(user.getModuleDtos());
        return curUserJsonMenus;
    }

    /**
     * 获取用户菜单信息
     *
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/getSubMenu")
    @ResponseBody
    public ReturnData getSubMenu(ModuleDto moduleDto) {
        UserDto user = (UserDto) sessionManager.getAttribute(Consts.USER);
        if (StringUtil.isNull(user)) {
            return new ReturnData("9999", "系统超时，请重新登录。");
        }
        return new ReturnData(Consts.SUCCESS, "", moduleServiceImpl.getSubMenu(moduleDto));
    }

    /**
     * @param menuCode 菜单编码
     * @return void 返回类型
     * @throws Exception
     * @Title: queryByCode
     * @Description: 根据ID获取数据对象
     * @Author: wangguoqing
     */
    @RequestMapping("/queryByCode")
    @ResponseBody
    public ReturnData queryByCode(@RequestParam(value = "menuCode",required = false) String menuCode) throws Exception {
        UserDto user = (UserDto) sessionManager.getAttribute(Consts.USER);
        if (StringUtil.isNull(user)) {
            return new ReturnData("9999", "系统超时，请重新登录。");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("menuCode", menuCode);
        List<ModuleDto> dto = moduleServiceImpl.listByConditionMap(map);
        if (dto == null) {
            return new ReturnData(Consts.ERROR, "没有找到对应的记录！", null);
        }
        return new ReturnData(Consts.SUCCESS, "", dto.get(0));
    }

    /**
     * 查询菜单模块树
     *
     * @param roleId 角色id
     * @return ReturnData
     * @throws Exception
     */
    @RequestMapping("/getTreeData")
    @ResponseBody
    public ReturnData getTreeData(@RequestParam(value = "roleId", required = false) String roleId) throws Exception {
        return new ReturnData(Consts.SUCCESS, "success", moduleServiceImpl.getTreeData(roleId));
    }


    /**
     * 查询列表数据
     *
     * @param moduleDto 请求参数
     * @return ReturnData
     * @throws Exception
     */
    @RequestMapping("/queryByMenuList")
    @ResponseBody
    public ReturnData queryByMenuList(ModuleDto moduleDto) throws Exception {
        return moduleServiceImpl.queryByList(moduleDto);
    }

    /**
     * @return String
     * @Title: toModify
     * @Description: 管理员修改密码跳转页面
     * @Author: wangguoqing
     */
    @RequestMapping("/toModify")
    public String toModify() {
        UserDto user = (UserDto) sessionManager.getAttribute(Consts.USER);
        if (StringUtil.isNull(user)) {
            throw new SessionException("登录超时！");
        }
        return "system/user/modify";
    }

    /**
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return void 返回类型
     * @throws Exception
     * @Title: modifyPassword
     * @Description: 首页修改密码
     * @Author: wangguoqing
     */
    @RequestMapping("/modifyPassword")
    @ResponseBody
    public ReturnData modifyPassword(@RequestParam(value = "oldPassword", required = false) String oldPassword,
                                     @RequestParam(value = "newPassword", required = false) String newPassword) throws Exception {
        UserDto user = (UserDto) sessionManager.getAttribute("user");
        if (StringUtil.isNull(user)) {
            return new ReturnData("9999", "系统超时，请重新登录。");
        }
        user.setPassword(newPassword);
        boolean modifyStatus = userServiceImpl.updatePassword(user, oldPassword);
        if (modifyStatus) {
            return new ReturnData(Consts.SUCCESS, "修改密码成功。", null);
        } else {
            return new ReturnData(Consts.ERROR, "修改密码失败，请联系管理人员。", null);
        }
    }

    /**
     * @return ReturnData 返回类型
     * @throws Exception
     * @Title: refresh
     * @Description: 刷新界面清空 returnObject
     * param response
     */
    @RequestMapping("/refresh")
    @ResponseBody
    public ReturnData refresh() throws Exception {
        return new ReturnData(Consts.SUCCESS, "成功~", null);
    }
}
